package org.club.Service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.club.Service.AnnouncementService;
import org.club.common.util.ApiResponseMe;
import org.club.mapper.AnnouncementMapper;
import org.club.common.model.dto.Announcemen;
import org.club.common.model.dto.AnnouncementsDto;
import org.club.common.model.entity.Announcement;
import org.club.common.model.entity.BusinessException;
import org.club.common.model.vo.AnnouncementUserVO;
import org.club.common.model.vo.AnnouncementVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AnnouncementServiceImpl implements AnnouncementService {
    @Autowired
    private AnnouncementMapper announcementMapper;

    //获取公告列表
    @Override
    public List<AnnouncementVO> getAllAnnouncements(AnnouncementsDto announcementsDto) {
        // 设置分页参数
        PageHelper.startPage(announcementsDto.getPageNum(), announcementsDto.getPageSize());
        // 执行查询
        List<AnnouncementVO> announcements = announcementMapper.getAllAnnouncements(announcementsDto);

        // 返回结果
        return announcements;
    }

    //更新公告
    @Override
    @Transactional
    public void updateAnnouncement(Announcement announcement) {
        // 检查必填字段
        if (announcement.getId() == null) {
            throw new BusinessException(400, "公告ID不能为空");
        }

        if (StringUtils.isEmpty(announcement.getTitle())) {
            throw new BusinessException(400, "公告标题不能为空");
        }
        if (StringUtils.isEmpty(announcement.getContent())) {
            throw new BusinessException(400, "公告内容不能为空");
        }
        // 检查公告是否存在
        Announcement existingAnnouncement = announcementMapper.selectById(announcement.getId());
        if (existingAnnouncement == null) {
            throw new BusinessException(400, "公告不存在");
        }
        
        // 设置更新时间
        announcement.setUpdatedAt(LocalDateTime.now());
        
        // 更新公告
        int result = announcementMapper.updateById(announcement);
        if (result != 1) {
            throw new BusinessException(500, "更新失败");
        }
    }

    @Override
    public void deleteAnnouncement(Long id) {
        if (id == null){
            throw new BusinessException(400, "id不能为空");
        }
        announcementMapper.deleteAnnouncement(id);
    }

    @Override
    public List<AnnouncementUserVO> searchUsers(String keyword) {
        List<AnnouncementUserVO> users = announcementMapper.searchUsers(keyword);
        if (users == null){
            throw new BusinessException(400, "用户不存在");
        }
        return users;
    }
//创建公告
    @Override
    public Long createAnnouncement(Announcemen announcemen) {
        if (announcemen.getTitle()==null||announcemen.getTitle().isEmpty()){
            throw new BusinessException(400, "公告标题不能为空");
        }
        if (announcemen.getContent()==null||announcemen.getContent().isEmpty()){
            throw new BusinessException(400, "公告内容不能为空");
        }
        if (announcemen.getType()==null||announcemen.getType().isEmpty()){
            throw new BusinessException(400, "公告类型不能为空");
        }
        if (announcemen.getSenderId()==null){
            throw new BusinessException(400, "创建人不能为空");
        }
        // 设置初始状态
        announcemen.setPublishStatus("CREATED");//CREATED-已创建, PUBLISHING-发布中, PUBLISHED-已发布, FAILED-发布失败';
        Integer id = announcementMapper.createANnouncements(announcemen);
        if (id==null||id<=0){
            throw new BusinessException(500, "数据库异常");
        }
        return announcemen.getId();
    }
}
